package edu.gatech.bsbmule.util;

import java.util.ArrayList;
import java.util.List;

import com.google.common.collect.ArrayTable;
import com.google.common.collect.Table;
import com.google.gson.Gson;

import edu.gatech.bsbmule.model.Game;

/**
 * Keeping records of game and player states for displaying statistics
 * 
 * @author BSB
 *
 */
public class StatisticsManager {
	
	private List<Integer> rowKeys;
	private List<String> columnKeys;
	
	private Table<Integer, String, StatisticsRecord> table;
	
	private static StatisticsManager instance;
	private static final Gson gson = new Gson();
	
	public static StatisticsManager getInstance() {
		if (instance == null) {
			instance = new StatisticsManager();
		}
		return instance;
	}
	
	
	/**
	 * Initialize the row keys of rounds and column keys of names
	 * 
	 * @param rounds maximum rounds
	 * @param names names of players
	 */
	public void initialize(int rounds, List<String> names) {
		
		rowKeys = new ArrayList<Integer>();
		
		// Create row keys that are 1 ... rounds integers
		for (int i = 0 ; i < rounds ; ++i) {
			rowKeys.add(i + 1);
		}
		
		columnKeys = names;
		
		table = ArrayTable.create(rowKeys, columnKeys);
	}
	
	/**
	 * Put a record into the table
	 * 
	 * @param round round number
	 * @param name name of the player
	 * @param record record of current player information
	 */
	public void putRecord(int round, String name, StatisticsRecord record) {
		table.put(round, name, record);
	}
	
	public Table<Integer, String, StatisticsRecord> getTable() {
		return table;
	}
	
	public static String saveToJson() {
		return gson.toJson(instance);
	}
	
	public static void loadFromJson(String json) {
		instance = gson.fromJson(json, StatisticsManager.class);
	}
}
